package cc.rengu.igas.share.core.realize.impl;

import cc.rengu.igas.share.common.constant.ShareServiceConstant;
import cc.rengu.igas.share.core.realize.SmpsService;
import cc.rengu.igas.smps.facade.base.Header;
import cc.rengu.igas.smps.facade.request.AlarmNoticeRequest;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.model.TxnInfo;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.TxnInfoService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.oltp.service.realize.impl.TxnInfoServiceImpl;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.GlobalTransUtil;
import cc.rengu.oltp.utility.util.RandomUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;

public class SmpsServiceImpl implements SmpsService {
    private final RgLogger rglog = RgLog.getLogger(this.getClass().getName());

    @Override
    public void pushRiskMessage(String riskMessage, String alaramLevel, String respCode) {
        try {
            XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
            String sysSeqNum = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.SYS_SEQ_NUM);
            TxnInfo txnInfo = (TxnInfo) xmlTreeUtil.getXmlTreeObjectValue(TreeNodeConstant.TXN_INFO,TxnInfo.class);
            DubboService dubboService = new DubboServiceImpl();
            AlarmNoticeRequest alarmNoticeRequest = new AlarmNoticeRequest();
            cc.rengu.igas.smps.facade.base.Header header = new Header();
            ConvertUtil.convertOutput(header);
            header.setTxnNum("0077000000RW01");
            header.setBizType(AppParamConstant.DEFAULT_BIZ_TYPE);
            header.setSrcSysId(AppParamConstant.SYS_ID);
            header.setTraceNo(sysSeqNum + RandomUtil.randomNumStr(4));
            alarmNoticeRequest.setHeader(header);
            alarmNoticeRequest.setAlarmLevel(alaramLevel);
            alarmNoticeRequest.setTransCode(txnInfo.getSrcTxnAuthCfg().getTxnNum());
            alarmNoticeRequest.setTransName(txnInfo.getSrcTxnAuthCfg().getTxnName());
            alarmNoticeRequest.setErrorCode(respCode);
            alarmNoticeRequest.setAlarmDesc(riskMessage);
            alarmNoticeRequest.setAlarmInfo(riskMessage);
            dubboService.asynCallDubboService(ShareServiceConstant.RISK_ALARM_SERVICE_NAME, ShareServiceConstant.ALARM_PLAT_SERVICE_METHOD, alarmNoticeRequest, ShareServiceConstant.ALARM_RESULT_PROC_SERVICE);
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("调用告警平台异常:<{}>", byteArrayOutputStream.toString());
        }
    }

    @Override
    public void pushRiskMessage(String riskMessage, String alaramLevel, String respCode, String txnNum, String txnName) {
        try {
            XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
            String sysSeqNum = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.SYS_SEQ_NUM);
            DubboService dubboService = new DubboServiceImpl();
            AlarmNoticeRequest alarmNoticeRequest = new AlarmNoticeRequest();
            cc.rengu.igas.smps.facade.base.Header header = new Header();
            ConvertUtil.convertOutput(header);
            header.setTxnNum("0077000000RW01");
            header.setBizType(AppParamConstant.DEFAULT_BIZ_TYPE);
            header.setSrcSysId(AppParamConstant.SYS_ID);
            header.setTraceNo(sysSeqNum + RandomUtil.randomNumStr(4));
            alarmNoticeRequest.setHeader(header);
            alarmNoticeRequest.setAlarmLevel(alaramLevel);
            alarmNoticeRequest.setTransCode(txnNum);
            alarmNoticeRequest.setTransName(txnName);
            alarmNoticeRequest.setErrorCode(respCode);
            alarmNoticeRequest.setAlarmDesc(riskMessage);
            alarmNoticeRequest.setAlarmInfo(riskMessage);
            dubboService.asynCallDubboService(ShareServiceConstant.RISK_ALARM_SERVICE_NAME, ShareServiceConstant.ALARM_PLAT_SERVICE_METHOD, alarmNoticeRequest, ShareServiceConstant.ALARM_RESULT_PROC_SERVICE);
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("调用告警平台异常:<{}>", byteArrayOutputStream.toString());
        }
    }
}
